package com.example.wangning.mycarofcountry.findcar.seniority;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.ReduceCarList;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class SeniorityAdapter extends RecyclerView.Adapter<SeniorityAdapter.SeniorityViewHolder>{
    private VolleySingleton volleySingleton;
    private List<ReduceCarList> reduceCarLists;
    private ImageLoader imageLoader;

    public SeniorityAdapter() {
        volleySingleton = VolleySingleton.getVolleyIntance();
        imageLoader = volleySingleton.getImageLoader();
    }
    public void addData(List<ReduceCarList> list){
        reduceCarLists = list;
        notifyDataSetChanged();
    }

    @Override
    public SeniorityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.findcar_senority_item,null);
        return new SeniorityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SeniorityViewHolder holder, int position) {
        if (reduceCarLists != null && reduceCarLists.size() > 0){
            ReduceCarList carList = reduceCarLists.get(position);

            volleySingleton.getBitmap(volleySingleton,carList.getSpecpic(),holder.specpic_Iv);
            holder.seriesname_Tv.setText(carList.getSeriesname());
            holder.specname_Tv.setText(carList.getSpecname());
            holder.dealerprice_Tv.setText(carList.getDealerprice() + "万");
            holder.fctprice_Tv.setText(carList.getFctprice() + "万");
            holder.reduceMoney_Tv.setText("降" + new BigDecimal(carList.getFctprice() -
                    carList.getDealerprice()).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue() + "万");
            holder.fctprice_Tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

        }

    }

    @Override
    public int getItemCount() {
        return reduceCarLists != null && reduceCarLists.size() > 0 ? reduceCarLists.size() : 0;
    }

    class SeniorityViewHolder extends RecyclerView.ViewHolder{
        private ImageView specpic_Iv;
        private TextView seriesname_Tv, specname_Tv, dealerprice_Tv, fctprice_Tv, reduceMoney_Tv,
                city, shortname, distance, orderrange;


        public SeniorityViewHolder(View itemView) {
            super(itemView);
            specpic_Iv = (ImageView) itemView.findViewById(R.id.specpic_Iv);
            seriesname_Tv = (TextView) itemView.findViewById(R.id.seriesname_Tv);
            specname_Tv = (TextView) itemView.findViewById(R.id.specname_Tv);
            dealerprice_Tv = (TextView) itemView.findViewById(R.id.dealerprice_Tv);
            fctprice_Tv = (TextView) itemView.findViewById(R.id.fctprice_Tv);
            reduceMoney_Tv = (TextView) itemView.findViewById(R.id.reduceMoney_Tv);
            city = (TextView) itemView.findViewById(R.id.city);
            shortname = (TextView) itemView.findViewById(R.id.shortname);
            distance = (TextView) itemView.findViewById(R.id.distance);
            orderrange = (TextView) itemView.findViewById(R.id.orderrange);
        }
    }
}
